/*
 * Copyright 2003,2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.jakubiak.jpa.translator;

import eu.jakubiak.jpa.translator.discriminator.Discriminator;
import eu.jakubiak.jpa.translator.discriminator.JpaDiscriminator;
import eu.jakubiak.jpa.translator.locale.LocaleParser;
import eu.jakubiak.jpa.translator.provider.JpaProvider;
import eu.jakubiak.jpa.translator.provider.Provider;
import eu.jakubiak.jpa.translator.serializer.PlainSerializer;
import eu.jakubiak.jpa.translator.serializer.Serializer;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author jakubiak_a
 */
public class TranslatorProperties {
    
    private static final Log log = LogFactory.getLog(TranslatorProperties.class);

    public static final String BUNDLE_NAME = "jpatranslator";
    public static final String KEY_PERSISTENCE_UNIT_NAME = "persistenceUnitName";
    public static final String KEY_DEFAULT_JPA_LOCALE = "defaultJpaLocale";
    public static final String KEY_DEFAULT_SERIALIZER = "defaultSerializer";
    public static final String KEY_DEFAULT_PROVIDER = "defaultProvider";
    public static final String KEY_DEFAULT_DISCRIMINATOR = "defaultDiscriminator";
    private static final Properties properties;
    
    private static final Class<? extends Serializer> serializerClass;
    private static final Class<? extends Provider> providerClass;
    private static final Class<? extends Discriminator> discriminatorClass;

    /**
     * Loading properties from properties file
     */
    static {
        properties = new Properties();
        ResourceBundle rb = ResourceBundle.getBundle(BUNDLE_NAME);
        for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
            final String key = (String) keys.nextElement();
            final String value = rb.getString(key);
            properties.put(key, value);
        }
        
        
        // get default locale
        defaultLocale = Locale.getDefault();
        String localeString = properties.getProperty(KEY_DEFAULT_JPA_LOCALE, null);
        if (localeString != null) {
            LocaleParser lp = new LocaleParser(localeString);
            if(null!=lp.getLocale()) {
                defaultLocale = lp.getLocale();
            }
        }
        
        serializerClass = getClassForKey(KEY_DEFAULT_SERIALIZER, PlainSerializer.class);
        providerClass = getClassForKey(KEY_DEFAULT_PROVIDER, JpaProvider.class);
        discriminatorClass = getClassForKey(KEY_DEFAULT_DISCRIMINATOR, JpaDiscriminator.class);
        
        
        

    }

    public static String getPersistenceUnitName() {
        return properties.getProperty(KEY_PERSISTENCE_UNIT_NAME, "translatorPU");
    }
    /**
     * Lazy load
     */
    public static Locale defaultLocale;

    /**
     * Default database locale
     * 
     * http://en.wikipedia.org/wiki/Locale
     * [language[_territory][.codeset][@modifier]]
     * @return
     */
    public static Locale getDefaultJpaLocale() {
        return defaultLocale;
    }
    

    /**
     * Get klass for key
     * @param key
     * @param defaultClass
     * @return
     */
    private static Class getClassForKey(String key, Class defaultClass) {
        String className = (String)properties.getProperty(key, null);
        if(className!=null) {
            try {
                return Class.forName(className);
            } catch(ClassNotFoundException e) {
                log.warn("Class not found: " + className + ", using default: " + defaultClass.getName() );
            }
        }
        return defaultClass;
    }

    public static Class<? extends Discriminator> getDiscriminatorClass() {
        return discriminatorClass;
    }

    public static Class<? extends Provider> getProviderClass() {
        return providerClass;
    }

    public static Class<? extends Serializer> getSerializerClass() {
        return serializerClass;
    }
    
    
    
}
